import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_module_sale/phoenix/message/first_message/bean/bean_message.dart';
import 'package:flutter/material.dart';
import '../../../../r.dart';

class MessageCellWidget extends StatelessWidget {
  final DataListBean dataListBean;
  final GestureTapCallback tapCallback;
  final int index;
  MessageCellWidget({this.dataListBean,this.tapCallback,this.index});
  @override
  Widget build(BuildContext context) {
    String defaultImage = R.imagesToolsToolIconPalceholderPng;
    return InkWell(
      onTap: this.tapCallback,
      child: Container(
        color: Colors.white,
        margin: this.index == 0 ? EdgeInsets.only(top: 10) : null,
        height: 77,
        child: Row(
          children: <Widget>[
            Container(
              width: 73,
              //color: Colors.green,
              child: Stack(
                children: <Widget>[
                  Container(
                    width: 44,
                    height: 44,
                    margin: EdgeInsets.only(left: 16,top: 16),
                    child: ClipOval(
                        child: CachedNetworkImage(
                            errorWidget: (context, url, error) => Image.asset(defaultImage,
                                height: 44,
                                width: 44,
                                fit: BoxFit.cover),
                            imageUrl: this.dataListBean.iconUrl ?? "",
                            fit: BoxFit.contain,
                            height: 44,
                            width: 44,
                            placeholder: (context, url) {
                              return Image.asset(defaultImage,
                                  height: 44,
                                  width: 44,
                                  fit: BoxFit.cover);
                            })),
                  ),
                  Center(
                    child: Container(
                      margin: EdgeInsets.only(left: 30,bottom: 37),
                      child: this.buildMessageCountWidget(),
                    )
                  )
                ],
              ),
            ),
            Expanded(
                child: Container(
                  margin: EdgeInsets.only(bottom: 0.5),
                  padding: EdgeInsets.only(right: 16),
                  decoration: BoxDecoration(
                      border:Border(bottom: Divider.createBorderSide(context,color: Color(0xFFEBEEEF),width: 0.5))
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Row(
                        children: <Widget>[
                          Expanded(
                            child: Padding(
                              padding: EdgeInsets.only(top: 19),
                              child: Text(
                                this.dataListBean.title,
                                  overflow:TextOverflow.ellipsis ,
                                style: TextStyle(
                                    color: Color(0xFF333333),
                                    fontSize: 16,
                                    fontWeight: FontWeight.w500
                                ),
                              ),
                            ),
                          ),
                          SizedBox(
                            width: 20,
                          ),
                          Padding(
                            child: Text(
                              this.dataListBean.date,
                              style: TextStyle(
                                  color: Color(0xFF333333),
                                  fontSize: 12
                              ),),
                            padding: EdgeInsets.only(top: 21),
                          )
                        ],
                      ),
                      SizedBox(
                        height: 6,
                      ),
                      Text(
                        this.dataListBean.subTitle,
                        style: TextStyle(
                            fontSize: 12,
                            color: Color(0xFF999999)
                        ),
                        overflow: TextOverflow.ellipsis,
                      )
                    ],
                  ),
                )
            )
          ],
        ),
      ),
    );
  }
  Widget buildOneMessage(int count) {
    return Container(
      alignment: Alignment.center,
      child: Text("$count",
        style: TextStyle(
          color: Colors.white,
          fontSize: 11,
        ),
      ),
      width: 16,
      height: 16,
      decoration: BoxDecoration(
          color: Color(0xFFFF5D6A),
          borderRadius: BorderRadius.circular(8)
      ),
    );
  }
  Widget buildMore99Message() {
    return Container(
      height: 14,
      decoration: BoxDecoration(
          color: Color(0xFFFF5D6A),
          borderRadius: BorderRadius.circular(8)
      ),
      child: Text(
        " 99+ ",
        style: TextStyle(
          color: Colors.white,
          fontSize: 11,
        ),
        //textAlign: TextAlign.center,
      ),
    );
  }

  Widget buildMessageCountWidget() {
    if(this.dataListBean.unReadNum == 0) {
      return Text("");
    } else if(this.dataListBean.unReadNum > 99){
      return buildMore99Message();
    } else {
      return buildOneMessage(this.dataListBean.unReadNum);
    }
  }
}
